﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;

namespace DAO
{
    public class KhachHangDAO : AbstractDAO<DTO.KhachHangDTO>
    {
        protected override string GetTableName()
        {
            return "khachhang";
        }

        public override DTO.KhachHangDTO GetByDataRow(System.Data.DataRow row)
        {
            DTO.KhachHangDTO khachhang = new DTO.KhachHangDTO();
            int id;
            String tendangnhap, matkhau, email, hoten, cmnd;
            int gioitinh;
            double no;
            DateTime ngaydangky;
            int.TryParse(row["id"].ToString(), out id);
            int.TryParse(row["gioitinh"].ToString(), out gioitinh);
            tendangnhap = row["tendangnhap"].ToString();
            matkhau = row["matkhau"].ToString();
            email = row["email"].ToString();
            hoten = row["hoten"].ToString();
            cmnd = row["cmnd"].ToString();
            DateTime.TryParse(row["ngaydangky"].ToString(), out ngaydangky);
            double.TryParse(row["no"].ToString(), out no);

            khachhang.Id = id;
            khachhang.Tendangnhap = tendangnhap;
            khachhang.Matkhau = matkhau;
            khachhang.Email = email;
            khachhang.Hoten = hoten;
            khachhang.Cmnd = cmnd;
            khachhang.Gioitinh = gioitinh;
            khachhang.Ngaydangky = ngaydangky;
            khachhang.No = no;

            return khachhang;
        }

        public DTO.KhachHangDTO GetByDataRow(System.Data.DataRow row, int mode)
        {
            DTO.KhachHangDTO khachhang = new DTO.KhachHangDTO();
            int id;
            String tendangnhap, matkhau, email, hoten, cmnd;
            int gioitinh;
            double no;
            DateTime ngaydangky;
            int.TryParse(row["id"].ToString(), out id);
            int.TryParse(row["gioitinh"].ToString(), out gioitinh);
            tendangnhap = row["tendangnhap"].ToString();
            matkhau = row["matkhau"].ToString();
            email = row["email"].ToString();
            hoten = row["hoten"].ToString();
            cmnd = row["cmnd"].ToString();
            DateTime.TryParse(row["ngaydangky"].ToString(), out ngaydangky);
            double.TryParse(row["no"].ToString(), out no);

            khachhang.Id = id;
            khachhang.Tendangnhap = tendangnhap;
            khachhang.Matkhau = matkhau;
            khachhang.Email = email;
            khachhang.Hoten = hoten;
            khachhang.Cmnd = cmnd;
            khachhang.Gioitinh = gioitinh;
            khachhang.Ngaydangky = ngaydangky;
            khachhang.No = no;
            khachhang.LaKHTT = row["laKHTT"].ToString() == "True";
            if (khachhang.LaKHTT)
            {
                khachhang.DiemThuong = int.Parse(row["diem"].ToString());
            }

            return khachhang;
        }

        public bool AddNew(DTO.KhachHangDTO khachhangDTO)
        {
            IList<SqlParameter> listParameter = new List<SqlParameter>();
            listParameter.Add(new SqlParameter("@TenDangNhap", khachhangDTO.Tendangnhap));
            listParameter.Add(new SqlParameter("@MatKhau", khachhangDTO.Matkhau));
            listParameter.Add(new SqlParameter("@GioiTinh", khachhangDTO.Gioitinh));
            listParameter.Add(new SqlParameter("@Email", khachhangDTO.Email));
            listParameter.Add(new SqlParameter("@HoTen", khachhangDTO.Hoten));
            listParameter.Add(new SqlParameter("@NgayDangKy", khachhangDTO.Ngaydangky));
            listParameter.Add(new SqlParameter("@No", khachhangDTO.No));
            listParameter.Add(new SqlParameter("@CMND", khachhangDTO.Cmnd));
            SqlParameter sqlThongBao = new SqlParameter("@ThanhCong", SqlDbType.Bit);
            sqlThongBao.Direction = System.Data.ParameterDirection.Output;
            listParameter.Add(sqlThongBao);

            this.dataProvider.ExecuteStoreProc(
                "Proc_ThemKhachHang",
                listParameter);
            return (bool)sqlThongBao.Value;
        }

        public bool Edit(DTO.KhachHangDTO khachhangDTO)
        {
            IList<SqlParameter> listParameter = new List<SqlParameter>();
            listParameter.Add(new SqlParameter("@Id", khachhangDTO.Id));
            listParameter.Add(new SqlParameter("@TenDangNhap", khachhangDTO.Tendangnhap));
            listParameter.Add(new SqlParameter("@MatKhau", khachhangDTO.Matkhau));
            listParameter.Add(new SqlParameter("@Email", khachhangDTO.Email));
            listParameter.Add(new SqlParameter("@HoTen", khachhangDTO.Hoten));
            listParameter.Add(new SqlParameter("@GioiTinh", khachhangDTO.Gioitinh));
            listParameter.Add(new SqlParameter("@CMND", khachhangDTO.Cmnd));
            listParameter.Add(new SqlParameter("@No", khachhangDTO.No));
            SqlParameter thongBao = new SqlParameter("@ThanhCong", System.Data.SqlDbType.Bit);
            thongBao.Direction = System.Data.ParameterDirection.Output;
            listParameter.Add(thongBao);

            this.dataProvider.ExecuteStoreProc(
                "Proc_CapNhatKhachHang",
                listParameter);
            return (bool)thongBao.Value;
        }

        public bool CheckUsername(String username)
        {

            return false;
        }

        public bool CheckEmail(String email)
        {

            return false;
        }

        public bool CheckCMND(String cmnd)
        {

            return false;
        }
    }
}
